﻿using ChiDao.Component.Utils.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace ChiDao.Component.Utils
{
    /// <summary>
    /// 表达式辅助操作类（动态构造Expression）
    /// 创建时间：2013-09-26
    /// </summary>
    public static class ExpressionHelper<T, TQueryModel>
        where T : class, new()
        where TQueryModel : class,new()
    {
        private static Type TType;
        //private static Type TQueryModelType;
        /// <summary>
        /// 构造方法
        /// </summary>
        //public ExpressionHelper()
        //{
        //    TType = typeof(T);
        //    //TQueryModelType = typeof(TQueryModel);
        //}

        /// <summary>
        /// 构造与表达式
        /// </summary>
        /// <param name="dictionary">构造源</param>
        /// <returns>lambda表达式</returns>
        public static Expression<Func<T, bool>> GetAndLambdaExpression(Dictionary<string, string> dictionary)
        {
            Expression expression_return = Expression.Constant(true);
            ParameterExpression expression_param = Expression.Parameter(TType, "p");
            foreach (string key in dictionary.Keys)
            {
                Expression temp = Expression.Equal(Expression.Call(Expression.Property(expression_param, TType.GetProperty(key)), TType.GetMethod("ToString")),
                    Expression.Constant(dictionary[key]));
                expression_return = Expression.And(expression_return, temp);
            }
            return (Expression<Func<T, bool>>)Expression.Lambda<Func<T, bool>>(expression_return, new ParameterExpression[] { expression_param });
        }

        /// <summary>
        /// 构造或表达式
        /// </summary>
        /// <param name="dictionary">构造源</param>
        /// <returns>Lambda表达式</returns>
        public static Expression<Func<T, bool>> GetOrLambdaExpression(Dictionary<string, string> dictionary)
        {
            Expression expression_return = Expression.Constant(false);
            ParameterExpression expression_param = Expression.Parameter(TType, "p");
            foreach (string key in dictionary.Keys)
            {
                Expression temp = Expression.Equal(Expression.Call(Expression.Property(expression_param, TType.GetProperty(key)), TType.GetMethod("ToString")),
                    Expression.Constant(dictionary[key]));
                expression_return = Expression.Or(expression_return, temp);
            }
            return (Expression<Func<T, bool>>)Expression.Lambda<Func<T, bool>>(expression_return, new ParameterExpression[] { expression_param });
        }

        //public static Expression<Func<T, bool>> GetLambdaExpression(TQueryModel queryModel, LogicType logicType)
        //{
            //Expression expression_return = Expression.Constant(false);
            //ParameterExpression expression_param = Expression.Parameter(TType, "p");
            ////foreach (string key in dictionary.Keys)
            ////{
            ////    Expression temp = Expression.Equal(Expression.Call(Expression.Property(expression_param, TType.GetProperty(key)), TType.GetMethod("ToString")),
            ////        Expression.Constant(dictionary[key]));
            ////    expression_return = Expression.Or(expression_return, temp);
            ////}

            //var tQueryModelProperties = queryModel.GetType().GetProperties();
            //foreach (var p in tQueryModelProperties)
            //{
            //    Expression temp = Expression.Equal(Expression.Call(Expression.Property(expression_param, TType.GetProperty(key)), TType.GetMethod("ToString")),
            //        Expression.Constant(dictionary[key]));
            //    expression_return = Expression.Or(expression_return, temp);
            //}
            //return (Expression<Func<T, bool>>)Expression.Lambda<Func<T, bool>>(expression_return, new ParameterExpression[] { expression_param });
        //}
    }
}